import Vue from 'vue'
import VueRouter from 'vue-router'
import BookView from '../views/BookView.vue'
import myStore from "@/store";
import axios from "axios";
import fa from "element-ui/src/locale/lang/fa";

Vue.use(VueRouter)

const routes = [
    {
        path: '/',
        redirect: "/index",
        component: BookView,
        children: [
            {
                path: 'mine',
                redirect: '/mine/info',
                component: () => import('../views/book/MineView'),
                children: [
                    {
                        path: 'order',
                        component: () => import('../views/book/mine/OrderView'),
                        meta: {
                            title: '订单中心',
                            requireLogin: true
                        }
                    },
                    {
                        path: 'info',
                        component: () => import('../views/book/mine/InfoView'),
                        meta: {
                            title: '个人中心',
                            requireLogin: true
                        }
                    },
                    {
                        path: 'phone',
                        component: () => import('../views/book/mine/BindPhoneView'),
                        meta: {
                            title: '绑定手机',
                            requireLogin: true
                        }
                    },
                    {
                        path: 'password',
                        component: () => import('../views/book/mine/ChangePasswordView'),
                        meta: {
                            title: '修改密码',
                            requireLogin: true
                        }
                    },
                    {
                        path: 'email',
                        component: () => import('../views/book/mine/BindEmailView'),
                        meta: {
                            title: '绑定手机',
                            requireLogin: true
                        }
                    },
                    {
                        path: 'collection',
                        component: () => import('../views/book/mine/CollectionView'),
                        meta: {
                            title: '我的收藏',
                            requireLogin: true
                        }
                    },
                    {
                        path: 'personalData',
                        component: () => import('../views/book/mine/PersonalDataView'),
                        meta: {
                            title: '个人信息',
                            requireLogin: true
                        }
                    },
                    {
                        path: 'address',
                        component: () => import('../views/book/mine/AddressView'),
                        meta: {
                            title: '我的收货地址',
                            requireLogin: true
                        }
                    },
                    {
                        path: 'realMe',
                        component: () => import('../views/book/mine/RealNameAuthentication'),
                        meta: {
                            title: '实名认证',
                            requireLogin: true
                        }
                    },
                    {
                        path: 'myBooks',
                        component: () => import('../views/book/mine/MyBooks'),
                        meta: {
                            title: '我买过的书',
                            requireLogin: true
                        }
                    },
                    {
                        path: 'coupon',
                        component: () => import('../views/book/mine/CouponView'),
                        meta: {
                            title: '优惠券',
                            requireLogin: true
                        }
                    }
                ],
                meta: {
                    title: '个人中心',
                    requireLogin: true
                }
            },
            {
                path: 'index',
                component: () => import('../views/book/IndexVew'),
                meta: {
                    title: '首页'
                }
            },
            {
                path: 'category',
                component: () => import('../views/book/CategoryView'),
                meta: {
                    title: '相关书籍'
                }
            },{
                path: 'buy',
                component: () => import('../views/book/BuyView'),
                meta: {
                    title: '结算页面'
                }
            },{
                path: 'pay',
                component: () => import('../views/book/PayView'),
                meta: {
                    title: '支付页面'
                }
            },
            {
                path: 'shop',
                component: () => import('../views/book/ShopView'),
                meta: {
                    title: '购物车'
                }
            },
            {
                path: 'detail',
                component: () => import('../views/book/DetailView'),
                meta: {
                    title: '详情页'
                }
            },
            {
                path: 'top',
                component: () => import('../views/book/TopView'),
                meta: {
                    title: '排行榜'
                }
            }
        ]
    },
    {
        path: '/login',
        component: () => import('../views/login/LoginView'),
        meta: {
            title: '登录页'
        }
    },
    {
        path: '/register',
        component: () => import('../views/login/RegisterView'),
        meta: {
            title: '注册页'
        }
    },
    {
        path: '/admin',
        component: () => import('../views/admin/AdminView'),
        meta: {
            title: '后台管理',
            requireLogin: true
        }
    },
    {
        path: '/t',
        component: () => import('../views/admin/TestView'),
        meta: {
            title: '测试',
            requireLogin: false
        }
    },
    {
        path: '/banner',
        component: () => import('../views/admin/BannerView'),
        meta: {
            title: '首页图片',
        }
    }
]
/*解决 报错 Navigation cancelled from “/roleList“ to “/userlist“ with a new navigation.）*/
const originalPush = VueRouter.prototype.push

VueRouter.prototype.push = function push (location) {

    return originalPush.call(this, location).catch(err => err)

}

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})
//验证是否需要拦截并跳转到登录页面
router.beforeEach((to, from, next) => {
    if (to.meta.title)
        document.title = to.meta.title
    if (to.meta.requireLogin) {
        if (myStore.state.isLogin===true){
            console.log("已经登录直接放行")
            next()
            return
        }
        let jwt = localStorage.getItem("Authentication");
        let userId = localStorage.getItem("uid");
        if (jwt === null) {
            //当前 jwt 为空直接跳转到登录界面
            next('/login')
        }
        //记录要去的 url 登录后进行回跳
        myStore.state.routerBeforeLoginTo = to.path
        axios.get( myStore.state.url + "/user/state/" + userId,
            {headers: {"Authentication": jwt}})
            .then(r => {
                //验证成功则 jwt 有效性，是否已经登录，未登录跳转到登录界面
                if (r.data.state === 20000){
                    console.log("登录有效")
                    myStore.state.isLogin = true
                    next()
                }
                if (r.data.state === 40400 || r.data.state === 40000) {
                    localStorage.removeItem("Authentication")
                    localStorage.removeItem("uid")
                    myStore.state.isLogin = false
                    next('/login')
                }
            }).catch(e => console.log(e))
    }
    //不需要验证登录直接放行
    next()
})

export default router
